package com.joke.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.joke.R;
import com.joke.model.Story;

public class JokeAdapter extends ArrayAdapter<Story> {

	private LayoutInflater inflater;
	private List<Story> itemList;
	private TextView itemTitle, itemText;
	private Context mContext;
	
	public JokeAdapter(Context context, List<Story> items) {
		super(context, R.layout.item, items);
		inflater = LayoutInflater.from(context);
		itemList = items;
		mContext = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View itemview = inflater.inflate(R.layout.item, null);
		itemTitle = (TextView)itemview.findViewById(R.id.itemTitle);
		itemText = (TextView)itemview.findViewById(R.id.itemText);
		if(itemList == null && itemList.size()==0){
			itemTitle.setText("");
			itemText.setText(mContext.getString(R.string.noDate));
		}else{
			itemTitle.setText(itemList.get(position).getTitle());
			//itemTitle.setTextColor(randTitColor());
			itemText.setText(itemList.get(position).getContent());
		}
		return itemview;
	}

	@Override
	public Story getItem(int position) {
		return itemList.get(position);
	}
	
	@Override
	public int getCount() {
		if(itemList!=null){
			return itemList.size();
		}
		return 0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public List<Story> getItemList() {
		return itemList;
	}

	public void setItemList(List<Story> itemList) {
		this.itemList = itemList;
	}
	
	public int randTitColor(){
		
		int[] mArrColor = new int[]{
				Color.WHITE,
				Color.RED,
				Color.BLUE
		};
		int iIndex = (int) (Math.random() * mArrColor.length);
		return mArrColor[iIndex];
	}
}
